package models;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import phong.musicplayer.R;
import utils.AsyncJSONDownloader;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;

public class SongData {

	static ArrayList<Song> songs;
	static Handler handler;
	public static int LOAD_COMPLETED = 10001;
	static Context context;

	public static void init(Handler loadSongHandler, Context contextz) {
		handler = loadSongHandler;
		context = contextz;
	}

	public static ArrayList<Song> getSongData() {
		if (songs == null) {
			songs = new ArrayList<Song>();
			loadSongData("https://dl.dropboxusercontent.com/u/182179436/songs.txt");
		}
		return songs;
	}

	static void loadSongData(String url) {
		AsyncJSONDownloader asynDownloader = new AsyncJSONDownloader(new Handler(
				new Callback() {
					@Override
					public boolean handleMessage(Message message) {
						if (message.what == AsyncJSONDownloader.DOWNLOAD_COMPLETE) {
							// Do download complete

							Bundle bundle = message.getData();
							String strJson = bundle.getString("response");

							if (strJson == null) {
								AlertDialog alertDialog = new AlertDialog.Builder(context).create();
								alertDialog.setTitle("Error");
								alertDialog.setMessage("Can't Connect To Server");
								alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
										new DialogInterface.OnClickListener() {

											@Override
											public void onClick(DialogInterface dialog, int which) {
												if (which == -1)
													dialog.cancel();
											}
										});
								alertDialog.setIcon(R.drawable.network_error);
								alertDialog.show();
								return false;
							}

							try {
								JSONObject jObject = new JSONObject(strJson);
								JSONArray jsonArraySong = jObject
										.getJSONArray("songs");

								for (int i = 0; i < jsonArraySong.length(); i++) {
									JSONObject jsonObjectSong = jsonArraySong
											.getJSONObject(i);
									// String category = jsonObjectSong
									// .getString("category");
									String songName = jsonObjectSong
											.getString("song_name");
									String singer = jsonObjectSong
											.getString("singer");
									String urlDownload = jsonObjectSong
											.getString("url_download");

									songs.add(new Song(songName, singer,
											urlDownload));
								}

								// send message LOAD_COMPLETED
								handler.sendEmptyMessage(LOAD_COMPLETED);

							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
						return false;
					}
				}));
		asynDownloader.execute(new String[] { url });

	}

	static void createSongDatabase() {
		songs.add(new Song(
				"Anh Yêu Người Khác Rồi",
				"Khắc Việt",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_AnhYeuNguoiKhacRoi-KhacViet.mp3"));
		songs.add(new Song(
				"Bức Tranh Từ Nước Mắt",
				"Mr Siro",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_BucTranhTuNuocMat-MrSiro.mp3"));
		songs.add(new Song("Cơn Mưa Ngang Qua", "MTP",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_ConMuaNgangQua-MTP.mp3"));
		songs.add(new Song(
				"Khi Người Lớn Cô Đơn",
				"Hồng Phước",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_KhiNguoiLonCoDon-HongPhuoc.mp3"));
		songs.add(new Song("Mây Và Núi", "The Bells",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_MayVaNui-TheBells.mp3"));
		songs.add(new Song("Nắng Ấm Xa Dần", "Sơn Tùng - MTP",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_NangAmXaDan-SonTung-MTP.mp3"));
		songs.add(new Song("Người Yêu Cũ", "Khởi My",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_NguoiYeuCu-KhoiMy.mp3"));
		songs.add(new Song("Sai Lầm Vẫn Là Anh", "The Men",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_SaiLamVanLaAnh-TheMen.mp3"));
		songs.add(new Song(
				"Thu Cuối",
				"Yanbi ft Mr.T ft Hằng Bingbong",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_ThuCuoi-YanBiftMrTftHangBingBong.mp3"));
		songs.add(new Song(
				"Tình Yêu Màu Nắng",
				"Đoàn Thúy Trang ft Big Daddy",
				"https://dl.dropboxusercontent.com/u/182179436/Tre_TinhYeuMauNang-DoanThuyTrangftBigDaddy.mp3"));
	}
}
